package com.t282.crm.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Chen
 * @since 2021-05-19
 */
@Controller
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private RestTemplate restTemplate;

    @ResponseBody
    @RequestMapping("/postStorage")
    public Map postProduct( int pageIndex,
                               String prodName,
                               String stkWarehouse){


        String url ="http://localhost:8081/sale/storage/getStkPage?pageIndex="+pageIndex;
        String url2 ="http://localhost:8081/sale/storage/getPro?";
        try {
            if (prodName!=null){
                prodName = URLDecoder.decode(URLDecoder.decode(prodName,"UTF-8"),"utf-8");
                url2+="prodName="+prodName;
            }
            System.out.println(prodName);
            if (stkWarehouse!=null){
                stkWarehouse = URLDecoder.decode(URLDecoder.decode(stkWarehouse,"UTF-8"),"utf-8");
                url+="&stkWarehouse="+stkWarehouse;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        String page = restTemplate.getForObject(
                url
                , String.class);

        String page2 = restTemplate.getForObject(
                url2
                , String.class);
//        String page = restTemplate.getForObject(
//                "http://localhost:8081/sale/saleproduct/page/"+pageIndex+
//                        ""+prodName+""+prodType+""+prodBatch
//                , String.class);

//        String page = restTemplate.postForObject(
//                "http://localhost:8081/sale/saleproduct/page2",request
//                , String.class);

        Map map = new HashMap();
        map.put("stkPage",page);
        map.put("proPage",page2);
        return map;
    }

    @GetMapping("/get")
    public String get(){
        return "storage/list";
    }
}

